/// <reference path="../../index.d.ts" />
const Service = require("egg").Service
class UserCache extends Service {
	constructor(ctx) {
		super(ctx)
		this.redis = this.ctx.app.redis
	}
	async getWxSession(oid) {
		let key = this.config.redisPrefix.wxUser + oid
		let redisData = await this.redis.get(key)
		if (!redisData)// 找不到缓存
			return undefined
		try {
			let wxUser = JSON.parse(redisData)
			return wxUser
		} catch (error) {
			return undefined
		}

	}
	/**
	 * 
	 * @param {number} oid 
	 * @param {object} obj 
	 * @param {Object} preVal 
	 */
	async setWxSession(oid, obj, preVal) {
		if (!(obj instanceof Object)) {
			return false
		}
		if (preVal instanceof Object) {
			obj = Object.assign(preVal, obj)
		}
		let key = this.config.redisPrefix.wxUser + oid
		await this.redis.set(key, JSON.stringify(obj))
		return true
	}
}
module.exports = UserCache
